<?php
namespace Common\Model;
use Common\Model\BaseModel;
/**
 * 权限规则model
 */
class AddPageModel extends BaseModel{

	/**
	 * 初始化
	 */
	public function __construct(){

	}

	/**
	 * 首页
	 */
	public function index(){


	}

	/**
	 * 创建单品页 - 列表
	 */
	public function singlepage(){

		$id = I("get.id",'','int');

		$typeid = I("get.typeid",'','int');

		//获取商品名称列表
		$where['user_id'] = array("eq",session('user.id'));

		$goods_list = M("goods")->field("id,goods_name")->where($where)->order("id desc")->select();

		//获取商品类目
		$GetClass  = D('Product')->GetClass();

		//获取基本信息
		if(!empty($id)){

			$map['a.id'] = array("eq",$id);
			$map['a.user_id'] = array("eq",session('user.id'));
			$info = M("addpage_info")->alias("a")->join("LEFT JOIN __GOODS__ b ON a.goods_id = b.id")->field("a.*,b.goods_name")->where($map)->find();

		}else{
			$info = array();
		}

		//获取显示数据
		$color_display = $this->color_display($id);

		$data = array(
			"id"=>$id,
			"typeid"=>$typeid,
			"goods_list"=>$goods_list,
			"getclass"=>$GetClass,
			"info"=>$info,
			"color_display"=>$color_display
		);

		return $data;

	}

	/**
	 * 商品套餐 - 列表
	 */
	public function singlepackage(){

		$id = I("get.id",'','int');
		$typeid = I("get.typeid",'','int');

		//获取商品套餐列表
		$where['addpage_info_id'] = array("eq",$id);

		//查询套餐列表
		$package_list = M("addpage_package")->where($where)->order("id asc")->select();

		$data = array();

		foreach($package_list as $k=>$list){

			$product_list = array();

			$data[$k] = $list;

			//查询产品列表
			$package_product = M("addpage_package_product")->where("package_id=".$list['id'])->order("id asc")->select();

			//查询属性列表
			foreach($package_product as $i=>$product){

				$attribute_list = array();

				$product_list[$i] = $product;

				//查询属性名称
				$attribute_name = M("addpage_package_attribute_name")->where("product_id=".$product['id'])->order("id asc")->select();

				foreach($attribute_name as $l=>$name){

					$attribute_list[$l] = $name;

					//查询属性值
					$attribute_list[$l]['attribute_val_list'] = M("addpage_package_attribute_val")->where("attribute_name_id=".$name['id'])->order("id asc")->select();

				}

				$product_list[$i]['attribute_name_list'] = $attribute_list;

			}

			$data[$k]['product_list'] = $product_list;

		}

		//获取显示数据
		$color_display = $this->color_display($id);

		$items = array(
			"id"=>$id,
			"typeid"=>$typeid,
			"package_list"=>$data,
			"color_display"=>$color_display
		);

		return $items;

	}

	/**
	 * 商品套餐 - 保存
	 */
	public function AjaxPackage(){

		$post = I("post.");

		//删除旧套餐数据
		$return = $this->PackageDel($post['addpage_info_id']);

		if($return['code'] == 200){

			foreach($post['addpage_package'] as $vo){

				//添加 - 商品套餐表
				$package_data = array(
					"package_name"=>$vo['package_name'],
					"package_price"=>$vo['package_price'],
					"addpage_info_id"=>$vo['addpage_info_id']
				);

				$id = M("addpage_package")->data($package_data)->add();

				foreach($vo['list'] as $k=>$list){

					//添加 - 商品产品表
					$product_data = array(
						"product_name"=>$list['product_name'],
						"package_id"=>$id
					);

					$product_id = M("addpage_package_product")->data($product_data)->add();

					foreach($list['attribute_name'] as $itmes){

						//添加 - 商品属性名称表
						$attribute_name_data = array(
							"product_id"=>$product_id,
							"attribute_name"=>$itmes['attribute_name']
						);

						$attribute_name_id = M("addpage_package_attribute_name")->data($attribute_name_data)->add();

						foreach($itmes['attribute_val'] as $val){

							//添加 - 商品属性值表
							$attribute_val_data = array(
								"attribute_name_id"=>$attribute_name_id,
								"attribute_val"=>$val
							);

							M("addpage_package_attribute_val")->data($attribute_val_data)->add();

						}

					}

				}

			}

			if(empty($id)){

				$data = array(
					"code"=>400,
					"msg"=>"系统繁忙，请稍后重试!"
				);

			}else{

				$data = array(
					"code"=>200,
					"msg"=>U('Admin/AddPage/singledetails',array('id'=>$post['addpage_info_id']))
				);

			}

		}else{
			$data = array(
				"code"=>400,
				"msg"=>"系统繁忙，请稍后重试!"
			);
		}

		return $data;

	}

	/**
	 * 商品套餐 - 删除
	 */
	public function PackageDel($id){

		try {
			//删除旧数据
			$where['addpage_info_id']  = array("eq",$id);

			$package_lists = M("addpage_package")->where($where)->select();

			foreach($package_lists as $list){

				$package_product = M("addpage_package_product")->where("package_id=".$list['id'])->select();

				foreach($package_product as $product){

					$attribute_name_list = M("addpage_package_attribute_name")->where("product_id=".$product['id'])->select();

					foreach($attribute_name_list as $name_list){
						M("addpage_package_attribute_val")->where("attribute_name_id=".$name_list['id'])->delete();
					}

					M("addpage_package_attribute_name")->where("product_id=".$product['id'])->delete();

				}

				M("addpage_package_product")->where("package_id=".$list['id'])->delete();

			}

			M("addpage_package")->where($where)->delete();

			$data = array(
				"code"=>200,
				"msg"=>"OK"
			);

		} catch( Exception $e ) {

			$data = array(
				"code"=>400,
				"msg"=>"系统繁忙，请稍后重试!"
			);
		}

		return $data;
	}


	/**
	 * 创建单品页 - 保存
	 */
	public function postsinglepage(){

		$post = I("post.");

		$data = array(
			"typeid"=>1,
			"goods_id"=>$post['goods_id'],
			"page_name"=>$post['page_name'],
			"page_title"=>$post['page_title'],
			"as_phone"=>$post['as_phone'],
			"page_img"=>$post['page_img'],
			"remarks"=>$post['remarks'],
			"page_edit_time"=>date("Y-m-d H:i:s",time()),
			"page_state"=>0,
			"user_id"=>session('user.id')
		);

		if(!empty($post['id'])){

			$where['id'] =  $post['id'];

			M("addpage_info")->where($where)->setField($data);

			$id = $post['id'];

		}else{

			$id = M("addpage_info")->data($data)->add();
		}

		$items = array(
			"id"=>$id,
		);

		return $items;

	}

	/**
	 * 验证 基本信息页面标题
	 */
	public function titleverify(){

		$id = I("post.id",'','int');
		$page_name = trim(I("post.page_name"));

		$where['user_id'] = array("eq",session('user.id'));
		$where['page_name']= array("eq",$page_name);

		if(!empty($id)){
			$where['id']= array("neq",$id);
		}

		$info = M("addpage_info")->field("id")->where($where)->find();

		if(!empty($info)){

			$data = array(
				"code"=>400,
				"msg"=>"页面名称不能重复",
			);

		}else{

			$data = array(
				"code"=>200,
				"msg"=>"OK",
			);

		}
		return $data;

	}


	/**
	 * 添加页面 - 基本信息
	 * 上传 - 页面主图
	 */
	public function UpdateImage(){

			if (IS_POST) {

				//生成用户目录
				$user_directory = crypt(session('user.id'),"Images");

				$config = array(
					'maxSize' => 2097152,
					'savePath' =>$user_directory.'/',
					'rootPath'     => './Upload/addpage/',
					'saveName' => array('uniqid', ''),
					'exts' => array('jpg', 'gif', 'png', 'jpeg'),
					'autoSub' => true,
					'subName' => array('date', 'Ymd'),
				);

				$upload = new \Think\Upload($config);

				$info = $upload->upload();

				if (!$info) {

					$data = array(
						"code"=>401,
						"msg"=>"上传失败,请检查文件格式或文件大小!"
					);

				} else {

					$src = ['src' => $info['file']['savepath'] . $info['file']['savename']];

					$img_base = '/Upload/addpage/'.$src['src'];

					$data = array(
						"code"=>200,
						"msg"=>$img_base
					);

					/*截图
                    $image = new \Think\Image();
                    $image->open($img_base);

                    if($image->width() < 270){
                        $image->thumb(371,258,\Think\Image::IMAGE_THUMB_FILLED)->save($img_base);
                    }else{
                        $image->thumb(371,258,\Think\Image::IMAGE_THUMB_FIXED)->save($img_base);
                    }

                    $this->ajaxReturn($src, 'json');*/

				}


			} else {

				$data = array(
					"code"=>400,
					"msg"=>"系统繁忙，请稍后重试!"
				);

			}

			return $data;

	}

	/**
	 * 添加页面 - 跳转
	 */
	public function jumpAjax(){

		$post = I("post.");
		//获取基本信息表

		$where['user_id'] = array("eq",session('user.id'));

		switch($post['page_id']){
			case 1:
				$where['id'] = array("eq",$post['id']);
				$info = M("addpage_info")->field("id")->where($where)->find();
				break;
			case 2:
				unset($where['user_id']);
				$where['addpage_info_id'] = array("eq",$post['id']);
				$info = M("addpage_package")->field("id")->where($where)->find();
				break;
			case 3:
				$where['addpage_info_id'] = array("eq",$post['id']);
				$info = M("addpage_details_index")->field("id")->where($where)->find();
		}

		switch($post['class_id']){
			case 1:
				$url = U('Admin/AddPage/singlepage',array('id'=>$post['id']));
				break;
			case 2:
				$url = U('Admin/AddPage/singlepackage',array('id'=>$post['id']));
				break;
			case 3:
				$url = U('Admin/AddPage/singledetails',array('id'=>$post['id']));
		}

		if(empty($info)){
			$data = array(
				"code"=>400,
				"msg"=>"请先保存当前页面信息",
			);
		}else{
			$data = array(
				"code"=>200,
				"msg"=>$url,
			);
		}

		return $data;

	}

	/**
	 * 商品详情页 - 列表
	 */
	public function singledetails(){

		$id = I("get.id",'','int');

		if(!empty($id)){
			//获取上传首图
			$where['b.typeid'] = array("eq",0);
			$where['b.addpage_info_id'] = array("eq",$id);
			$where['b.user_id'] = array("eq",session('user.id'));
			$index_images = M("addpage_details_images")->alias("a")->field("a.image")->join("LEFT JOIN __ADDPAGE_DETAILS_IMAGES_SERIAL__ b ON a.images_serial_id = b.id")->where($where)->order("a.id asc")->select();

			//获取按钮设置
			$details_index = M("addpage_details_index")->where("addpage_info_id=".$id)->find();

			//获取图文设置
			$where = array();
			$where['typeid'] = array("in",array(1,2));
			$where['addpage_info_id'] = array("eq",$id);
			$where['user_id'] = array("eq",session('user.id'));
			$images_serial_list = M("addpage_details_images_serial")->field("id,typeid")->where($where)->order("id asc")->select();

			$images_text = array();
			$text_num = $img_num = 0;

			foreach($images_serial_list as $k=>$list){

				$images_text[$k]['typeid'] = $list['typeid'];

				if($list['typeid'] == 1){
					$images_text[$k]['images'] = M("addpage_details_images")->field("image")->where("images_serial_id=".$list['id'])->order("id asc")->select();
					$images_text[$k]['num'] = $img_num;
					$img_num++;
				}else{

					$images_text[$k]['text'] = M("addpage_details_text")->where("images_serial_id=".$list['id'])->order("id asc")->getField("text");
					$images_text[$k]['num'] = $text_num;
					$text_num++;
				}
			}

			//获取按钮设置
			$details_button = M("addpage_details_button")->where("addpage_info_id=".$id)->find();

			//获取商品套餐列表
			$where = array();
			$where['addpage_info_id'] = array("eq",$id);

			//查询套餐列表
			$package_list = M("addpage_package")->where($where)->select();

			$package_data = array();

			foreach($package_list as $k=>$list){

				$product_list = array();

				$package_data[$k] = $list;

				//查询产品列表
				$package_product = M("addpage_package_product")->where("package_id=".$list['id'])->select();

				//查询属性列表
				foreach($package_product as $i=>$product){

					if($i == 0){

						$attribute_list = array();

						$product_list[$i]['product_name'] = $product['product_name'];

						//查询属性名称
						$attribute_name = M("addpage_package_attribute_name")->where("product_id=".$product['id'])->select();

						foreach($attribute_name as $l=>$name){

							$attribute_list[$l] = $name;

							//查询属性值
							$attribute_list[$l]['attribute_val_list'] = M("addpage_package_attribute_val")->where("attribute_name_id=".$name['id'])->select();

						}

						$product_list[$i]['attribute_name_list'] = $attribute_list;

					}
				}

				$package_data[$k]['product_list'] = $product_list;

			}

			//获取基本信息 - 售后电话
			$where = array();
			$where['id'] = array("eq",$id);
			$where['user_id'] = array("eq",session('user.id'));
			$addpage_info = M("addpage_info")->field("as_phone")->where($where)->find();


			//获取省市数据
			$city_area = array();

			//解决缓存拖慢速度
			$province_city_area = F('province_city_area');

			if(!empty($province_city_area)){

				foreach($province_city_area as $k=>$area_list){

					$city_area[$k] = $area_list;

					//获取县数据
					$city_data = array();
					foreach($area_list['_data'] as $i=>$city){
						$city_data[$i] = $city;
						$city_data[$i]['county_list'] =  $city['_data'];

					}

					$city_area[$k]['city_list'] = $city_data;

				}

			}else{

				$province_city_area_list = $this->province_city_area(0);

				foreach($province_city_area_list as $k=>$area_list){

					$city_area[$k] = $area_list;

					//获取市数据
					$city_list =  $this->province_city_area($area_list['id']);

					//获取县数据
					$city_data = array();
					foreach($city_list as $i=>$city){
						$city_data[$i] = $city;
						$city_data[$i]['county_list'] =  $this->province_city_area($city['id']);
					}

					$city_area[$k]['city_list'] = $city_data;

				}

			}

		}

		//获取客服 - QQ号
		$qq = M("users")->where("id=".session('user.id'))->getField("qq");

		//获取显示数据
		$color_display = $this->color_display($id);

		$data = array(
			"id"=>$id,
			"index_images"=>$index_images,
			"details_index"=>$details_index,
			"images_text"=>$images_text,
			"img_num"=>$img_num,
			"text_num"=>$text_num,
			"details_button"=>$details_button,
			"package_data"=>$package_data,
			"addpage_info"=>$addpage_info,
			"city_area"=>$city_area,
			"qq"=>$qq,
			"color_display"=>$color_display
		);

		return $data;

	}

	/**
	 * 省市区数据获取
	 */
	public function province_city_area($pid = 0){

		$data = M("province_city_area")->where("pid=".$pid)->order("id asc")->select();

		return $data;

	}

	/**
	 * 商品详情页 - 列表
	 * 获取市县数据
	 */
	public function AjaxCityArea(){

		$id = I("post.id",'','int');

		//获取市数据
		$city_list =  $this->province_city_area($id);

		//获取县数据
		$city_data = array();
		foreach($city_list as $i=>$city){

			$city_data[$i] = $city;
			$city_data[$i]['county_list'] =  $this->province_city_area($city['id']);

		}

		return $city_data;
	}

	/**
	 * 商品详情页 - 保存
	 */
	public function AjaxDetails(){

		$post = I("post.",'','stripslashes,trim');

		try {

			//删除旧数据
			if(!empty($post['id'])){

				$where = array(
					"addpage_info_id"=>$post['id'],
					"user_id"=>session('user.id')
				);

				$images_serial_list = M("addpage_details_images_serial")->where($where)->select();

				foreach($images_serial_list as $serial_list){

					if($serial_list['typeid'] == 0 || $serial_list['typeid'] == 1){

						$serial_image_id[] = $serial_list['id'];
					}else{
						$serial_text_id[] = $serial_list['id'];
					}
				}
				//删除图片
				if(!empty($serial_image_id)){
					$mpa = array();
					$mpa['images_serial_id'] = array("in",$serial_image_id);
					M("addpage_details_images")->where($mpa)->delete();
				}

				//删除文字
				if(!empty($serial_text_id)){
					$mpa = array();
					$mpa['images_serial_id'] = array("in",$serial_text_id);
					M("addpage_details_text")->where($mpa)->delete();
				}

				//删除设置ID
				M("addpage_details_images_serial")->where($where)->delete();

				//删除首页设置表
				M("addpage_details_index")->where($where)->delete();

				//删除图文设置表
				M("addpage_details_button")->where($where)->delete();

			}

			//获取上传首图
			if(!empty($post['index_image'])){

				$details_images_serial_data = array(
					"addpage_info_id"=>$post['id'],
					"typeid"=>0,
					"user_id"=>session('user.id')
				);

				//获取图片设置类型
				$images_serial_id = M("addpage_details_images_serial")->data($details_images_serial_data)->add();

				//上传图片
				foreach($post['index_image'] as $image){
					$details_images_data = array(
						"image"=>$image,
						"images_serial_id"=>$images_serial_id
					);

					M("addpage_details_images")->data($details_images_data)->add();

				}

			}

			if($post['index_buy_hide'] == 1){
				$post['index_buy_height'] = $post['index_buy_height'] ? $post['index_buy_height'] : 45;
			}

			//插入首页设置表
			$details_index_data = array(
				"index_buy_name"=>$post['index_buy_name'],
				"index_price"=>$post['index_price'],
				"index_buy_bg_color"=>$post['index_buy_bg_color'],
				"index_buy_text_color"=>$post['index_buy_text_color'],
				"index_buy_height"=>$post['index_buy_height'],
				"index_buy_hide"=>$post['index_buy_hide'],
				"index_price_text_color"=>$post['index_price_text_color'],
				"index_price_hide"=>$post['index_price_hide'],
				"index_spee_number"=>$post['index_spee_number'],
				"addpage_info_id"=>$post['id'],
				"user_id"=>session('user.id')
			);

			if($post['index_buy_hide'] == 0){
				unset($details_index_data['index_buy_name']);
			}
			if($post['index_price_hide'] == 0){
				unset($details_index_data['index_price']);
			}

			M("addpage_details_index")->data($details_index_data)->add();

			//插入图文设置表
			if(!empty($post['image_text'])){

				//上传图片
				foreach($post['image_text'] as $item){

					if($item['typeid'] == 1){
						$typeid = 1;
					}else{
						$typeid = 2;
					}

					$details_images_serial_data = array(
						"addpage_info_id"=>$post['id'],
						"typeid"=>$typeid,
						"user_id"=>session('user.id')
					);

					//获取图片设置类型
					$images_serial_id = M("addpage_details_images_serial")->data($details_images_serial_data)->add();

					//图片类型
					if($item['typeid'] == 1){

						foreach($item['images'] as $image){

							$details_images_data = array(
								"image"=>$image,
								"images_serial_id"=>$images_serial_id
							);

							M("addpage_details_images")->data($details_images_data)->add();

						}

					}else{

						$details_text_data = array(
							"text"=>$item['text'],
							"images_serial_id"=>$images_serial_id
						);

						M("addpage_details_text")->data($details_text_data)->add();


					}

				}

			}
			//插入按钮设置
			$details_button_data = array(
				"button_form_name"=>$post['button_form_name'],
				"button_form_height"=>$post['button_form_height'],
				"button_phone_name"=>$post['button_phone_name'],
				"button_phone_number"=>$post['button_phone_number'],
				"button_phone_hide"=>$post['button_phone_hide'],
				"button_sms_name"=>$post['button_sms_name'],
				"button_sms_number"=>$post['button_sms_number'],
				"button_sms_hide"=>$post['button_sms_hide'],
				"button_buy_name"=>$post['button_buy_name'],
				"button_buy_hide"=>$post['button_buy_hide'],
				"button_bg_color"=>$post['button_bg_color'],
				"button_text_color"=>$post['button_text_color'],
				"button_qq_hide"=>$post['button_qq_hide'],
				"addpage_info_id"=>$post['id'],
				"user_id"=>session('user.id')
			);

			if($post['button_phone_hide'] == 0){
				unset($details_button_data['button_phone_number']);
			}
			if($post['button_sms_hide'] == 0){
				unset($details_button_data['button_sms_number']);
			}
			if($post['button_buy_hide'] == 0){
				unset($details_button_data['button_buy_name']);
			}


			M("addpage_details_button")->data($details_button_data)->add();

			if($post['lang'] == 2){

				$url = "http://".$_SERVER['HTTP_HOST'].U('Home/Mate/index',array('id'=>$post['id']));

			}else{

				//修改发布状态
				$where = array();
				$where['id'] = array("eq",$post['id']);

				M("addpage_info")->where($where)->setField('page_state',$post['page_state']);

				$url = U('Admin/Page/index');
			}

			if($post['lang'] == 0){
				$msg = "保存成功";
			}else if($post['lang'] == 1){
				$msg = "发布成功";
			}else{
				$msg = "系统繁忙，请稍后重试!";
			}

			$data = array(
				"code"=>200,
				"lang"=>$post['lang'],
				"url"=>$url,
				"msg"=>$msg
			);

		} catch ( Exception $e ) {
			$data = array(
				"code"=>400,
				"msg"=>""
			);
		}

		return $data;
	}

	//验证发布状态
	public function state_confirmation(){

		$id = I("get.id",'','int');

		if(!empty($id)){

			$where['id'] = array("eq",$id);

			$info = M("addpage_info")->field('page_state')->where($where)->find();

			if($info['page_state'] == 1){
				$data['code'] = 400;
				$data['msg'] = "该商品已发布,不能再编辑";

			}else{
				$data['code'] = 200;
				$data['msg'] = "OK";
			}

		}else{
			$data['code'] = 200;
			$data['msg'] = "OK";
		}


		return $data;
	}

	/**
	 * 创建聚合页 - 列表
	 */
	public function grouppage(){

		$id = I("get.id",'','int');

		$typeid = I("get.typeid",'','int');

		//获取基本信息
		if(!empty($id)){

			$map['a.id'] = array("eq",$id);
			$map['a.user_id'] = array("eq",session('user.id'));
			$info = M("addpage_info")->alias("a")->join("LEFT JOIN __GOODS__ b ON a.goods_id = b.id")->field("a.*,b.goods_name")->where($map)->find();

		}else{
			$info = array();
		}

		//获取显示数据
		$color_display = $this->color_display($id,2);

		$data = array(
			"id"=>$id,
			"typeid"=>$typeid,
			"info"=>$info,
			"color_display"=>$color_display
		);

		return $data;

	}

	/**
	 * 创建聚合页 - 保存
	 */
	public function grouppagesave(){

		$post = I("post.");

		$data = array(
			"typeid"=>2,
			"page_name"=>$post['page_name'],
			"page_title"=>$post['page_title'],
			"as_phone"=>$post['as_phone'],
			"page_img"=>$post['page_img'],
			"remarks"=>$post['remarks'],
			"page_edit_time"=>date("Y-m-d H:i:s",time()),
			"page_state"=>0,
			"user_id"=>session('user.id')
		);

		if(!empty($post['id'])){

			$where['id'] =  $post['id'];

			M("addpage_info")->where($where)->setField($data);

			$id = $post['id'];

		}else{

			$id = M("addpage_info")->data($data)->add();
		}

		$items = array(
			"id"=>$id,
		);

		return $items;

	}

	/**
	 * 聚合 - 商品详情页
	 */
	public function groupdetails(){

		$id = I("get.id",'','int');

		if(!empty($id)){

			//获取图文设置
			$where = array();
			$where['typeid'] = array("in",array(1,2,3));
			$where['addpage_info_id'] = array("eq",$id);
			$where['user_id'] = array("eq",session('user.id'));
			$images_serial_list = M("addpage_details_images_serial")->field("id,typeid,class_id")->where($where)->order("id asc")->select();

			$images_text = array();
			$group_num = $text_num = $img_num = 0;

			foreach($images_serial_list as $k=>$list){

				$images_text[$k]['typeid'] = $list['typeid'];

				if($list['typeid'] == 1){
					$images_text[$k]['images'] = M("addpage_details_images")->field("image")->where("images_serial_id=".$list['id'])->order("id asc")->select();
					$images_text[$k]['num'] = $img_num;
					$img_num++;
				}else if($list['typeid'] == 2){

					$images_text[$k]['text'] = M("addpage_details_text")->where("images_serial_id=".$list['id'])->order("id asc")->getField("text");
					$images_text[$k]['num'] = $text_num;
					$text_num++;

				}else{

					$addpage_group = M("addpage_group")->alias("a")->field("a.addpage_info_id,b.page_img")->join("LEFT JOIN __ADDPAGE_INFO__ b ON a.addpage_info_id = b.id")->where("a.images_serial_id=".$list['id'])->order("a.id asc")->select();
					$images_text[$k]['addpage_group'] = $addpage_group;
					$images_text[$k]['class_id'] = $list['class_id'];
					$images_text[$k]['num'] = $group_num;
					$group_num++;

				}
			}

			//获取基本信息 - 售后电话
			$where = array();
			$where['id'] = array("eq",$id);
			$where['user_id'] = array("eq",session('user.id'));
			$addpage_info = M("addpage_info")->field("as_phone")->where($where)->find();

			//获取聚合商品列页
			$map['user_id'] = array("eq",session('user.id'));
			$map['page_state'] = array("eq",1);
			$map['typeid'] = array("eq",1);
			$group_info = M("addpage_info")->field("id,page_name,page_img")->where($map)->select();

		}

		//获取显示数据
		$color_display = $this->color_display($id,2);

		$data = array(
			"id"=>$id,
			"images_text"=>$images_text,
			"img_num"=>$img_num,
			"text_num"=>$text_num,
			"group_num"=>$group_num,
			"addpage_info"=>$addpage_info,
			"group_info"=>$group_info,
			"color_display"=>$color_display
		);

		return $data;

	}

	/**
	 * 搜索聚合商品页
	 *
	 */
	public function search(){

		$keyword = I('post.keyword');

		if(!empty($keyword)){
			$map['page_name'] = array('like',"%".$keyword."%");
		}

		//获取聚合商品页
		$map['user_id'] = array("eq",session('user.id'));
		$map['page_state'] = array("eq",1);

		$group_info = M("addpage_info")->field("id,page_name,page_img")->where($map)->select();

		$html = '';

		foreach($group_info as $list){

			$html .= '<div class="ResetImgListCon">';
			$html .= '<i alt="'.$list['id'].'"></i>';
			$html .= '<img src="'.$list['page_img'].'"/>';
			$html .= '<div class="pic-document">'.$list['page_name'].'</div>';
			$html .= '</div>';
		}

		$data = array(
			'html'=>$html,
			'total'=>count($group_info)
		);


		return $data;
	}


	/**
	 * 聚合商品页 - 保存
	 */
	public function AjaxGroup(){

		$post = I("post.",'','stripslashes,trim');

		try {

			//删除旧数据
			if(!empty($post['id'])){

				$where = array(
					"addpage_info_id"=>$post['id'],
					"user_id"=>session('user.id')
				);

				$images_serial_list = M("addpage_details_images_serial")->where($where)->select();

				foreach($images_serial_list as $serial_list){

					if($serial_list['typeid'] == 0 || $serial_list['typeid'] == 1){
						$serial_image_id[] = $serial_list['id'];
					}else if($serial_list['typeid'] == 3){
						$serial_group_id[] = $serial_list['id'];
					}else{
						$serial_text_id[] = $serial_list['id'];
					}
				}
				//删除图片
				if(!empty($serial_image_id)){
					$mpa = array();
					$mpa['images_serial_id'] = array("in",$serial_image_id);
					M("addpage_details_images")->where($mpa)->delete();
				}

				//删除文字
				if(!empty($serial_text_id)){
					$mpa = array();
					$mpa['images_serial_id'] = array("in",$serial_text_id);
					M("addpage_details_text")->where($mpa)->delete();
				}

				//删除聚合页面
				if(!empty($serial_group_id)){
					$mpa = array();
					$mpa['images_serial_id'] = array("in",$serial_group_id);
					M("addpage_group")->where($mpa)->delete();
				}

				//删除设置ID
				M("addpage_details_images_serial")->where($where)->delete();

			}

			//插入图文设置表
			if(!empty($post['image_text'])){

				//上传图片
				foreach($post['image_text'] as $item){

					$class_id = isset($item['class_id']) ? $item['class_id'] : "";

					$details_images_serial_data = array(
						"addpage_info_id"=>$post['id'],
						"typeid"=>$item['typeid'],
						"user_id"=>session('user.id'),
						"class_id"=>$class_id
					);

					//获取图片设置类型
					$images_serial_id = M("addpage_details_images_serial")->data($details_images_serial_data)->add();

					//图片类型
					if($item['typeid'] == 1){

						foreach($item['images'] as $image){

							$details_images_data = array(
								"image"=>$image,
								"images_serial_id"=>$images_serial_id
							);

							M("addpage_details_images")->data($details_images_data)->add();

						}

					}elseif($item['typeid'] == 2){

						$details_text_data = array(
							"text"=>$item['text'],
							"images_serial_id"=>$images_serial_id
						);

						M("addpage_details_text")->data($details_text_data)->add();


					}else{

						foreach($item['addpage_info_id'] as $addpage_id){

							$details_images_data = array(
								"addpage_info_id"=>$addpage_id,
								"images_serial_id"=>$images_serial_id
							);

							M("addpage_group")->data($details_images_data)->add();

						}

					}

				}

			}

			if($post['lang'] == 2){

				$url = "http://".$_SERVER['HTTP_HOST'].U('Home/Group/index',array('id'=>$post['id']));

			}else{

				//修改发布状态
				$where = array();
				$where['id'] = array("eq",$post['id']);

				M("addpage_info")->where($where)->setField('page_state',$post['page_state']);

				$url = U('Admin/Page/index');
			}

			if($post['lang'] == 0){
				$msg = "保存成功";
			}else if($post['lang'] == 1){
				$msg = "发布成功";
			}else{
				$msg = "系统繁忙，请稍后重试!";
			}

			$data = array(
				"code"=>200,
				"lang"=>$post['lang'],
				"url"=>$url,
				"msg"=>$msg
			);

		} catch ( Exception $e ) {
			$data = array(
				"code"=>400,
				"msg"=>""
			);
		}

		return $data;
	}


	/**
	 * 聚合页面 - 跳转
	 */
	public function GroupjumpAjax(){

		$post = I("post.");
		//获取基本信息表
		$where['user_id'] = array("eq",session('user.id'));

		switch($post['page_id']){
			case 1:
				$where['id'] = array("eq",$post['id']);
				$info = M("addpage_info")->field("id")->where($where)->find();
				break;
			case 2:
				$where['addpage_info_id'] = array("eq",$post['id']);
				$info = M("addpage_details_images_serial")->field("id")->where($where)->find();
				break;
		}

		switch($post['class_id']){
			case 1:
				$url = U('Admin/AddPage/grouppage',array('id'=>$post['id']));
				break;
			case 2:
				$url = U('Admin/AddPage/groupdetails',array('id'=>$post['id']));
				break;
		}

		if(empty($info)){
			$data = array(
				"code"=>400,
				"msg"=>"请先保存当前页面信息",
			);
		}else{
			$data = array(
				"code"=>200,
				"msg"=>$url,
			);
		}

		return $data;

	}

	/**
	 * 查询单页品
	 * 按钮颜色显示
	 */
	public function color_display($id,$typeid=1){

		if(!empty($id)){

			//默认单品页
			if($typeid == 1){

				//基本信息页
				$where = array();
				$where['id'] = array("eq",$id);
				$addpage_info = M("addpage_info")->field("id")->where($where)->find();

				//查询商品套餐
				$where = array();
				$where['addpage_info_id'] = array("eq",$id);
				$package_info = M("addpage_package")->field("id")->where($where)->find();

				//查询商品详情页
				$where = array();
				$where['addpage_info_id'] = array("eq",$id);
				$images_serial = M("addpage_details_images_serial")->field("id")->where($where)->find();
				$details_button = M("addpage_details_button")->field("id")->where($where)->find();

				$one = $addpage_info ? 1 : 0;

				$two = $package_info ? 1 :0;

				$three = ($images_serial || $details_button) ? 1 : 0;

				$data = array('one'=>$one,'two'=>$two,'three'=>$three);


			}else{

				//基本信息页
				$where = array();
				$where['id'] = array("eq",$id);
				$addpage_info = M("addpage_info")->field("id")->where($where)->find();

				//查询商品详情页
				$where = array();
				$where['addpage_info_id'] = array("eq",$id);
				$images_serial = M("addpage_details_images_serial")->field("id")->where($where)->find();

				$one = $addpage_info ? 1 : 0;

				$two = $images_serial ? 1 :0;

				$data = array('one'=>$one,'two'=>$two);

			}

		}else{

			if($typeid == 1){
				$data = array('one'=>0,'two'=>0,'three'=>0);
			}else{
				$data = array('one'=>0,'two'=>0);
			}
		}

		return $data;

	}


}
